classdef dotsTheQueryablesManager < dotsAllObjectManagers
    % @class dotsTheQueryablesManager
    % Singleton to manage subclasses of dotsAllQueryables
    % @details
    % All subclasses of dotsAllQueryables should be created with the
    % newObject factory method.  This allows the manager to
    % determine whether a new queryable object needs to be created or
    % whether an existing object can be resused.
    
    methods (Access=private)
        function self = dotsTheQueryablesManager
            self.initialize;
        end
    end
    
    methods (Static)
        function self = theObject
            persistent qm
            if isempty(qm) || ~isvalid(qm)
                qm = dotsTheQueryablesManager;
            end
            self = qm;
        end
        
        function reset
            self = dotsTheQueryablesManager.theObject;
            self.initialize;
        end
        
        function g = gui
            self = dotsTheQueryablesManager.theObject;
            g = self.managedObjects.gui;
        end
    end
    
    methods
        % Restore the manager singleton to a fresh state.
        function initialize(self)            
            self.initialize@dotsAllObjectManagers;
            dotsTheMachineConfiguration.applySettingsToObject(self);
        end

        % Restrict instances to one per class
        function object = newObject(self, className)
            if any(strcmp(self.managedObjects.groups, className))
                objects = self.managedObjects.getAllItemsFromGroup(className);
                object = objects{1};
                
                % attempt to return useful object
                if ~object.isAvailable
                    object.initialize;
                end
            else
                object = self.newObject@dotsAllObjectManagers(className);
            end
        end
    end
end